package com.jiutong.meipenggang.ui.activitys;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiutong.meipenggang.R;
import com.jiutong.meipenggang.base.BaseActivity;
import com.jiutong.meipenggang.common.Constants;
import com.jiutong.meipenggang.entity.PayOut1Bean;
import com.jiutong.meipenggang.entity.SurePayOutBean;
import com.jiutong.meipenggang.http.model.interfaces.MeiCaiBaoBiz;
import com.jiutong.meipenggang.http.present.MeiCaiBaoPresent;
import com.jiutong.meipenggang.utils.SpConfig;
import com.jiutong.meipenggang.views.dialog.BaseDialog;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PayOutActivity extends BaseActivity implements MeiCaiBaoBiz {

    @Bind(R.id.rl_common_title_back)
    RelativeLayout rlCommonTitleBack;
    @Bind(R.id.tv_common_title)
    TextView tvCommonTitle;
    @Bind(R.id.editText)
    EditText editText;
    @Bind(R.id.btn_sure_payout)
    Button btnSurePayout;
    @Bind(R.id.payout1_balance)
    TextView payout1Balance;
    @Bind(R.id.payout1_can_pay)
    TextView payout1CanPay;

    private BaseDialog dialog;
    private MeiCaiBaoPresent meiCaiBaoPresent;
    private String amount;
    private double balance;


    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_pay_out;
    }


    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        tvCommonTitle.setText("转出");
    }

    @Override
    protected void initDate(Bundle savedInstanceState) {
        meiCaiBaoPresent = new MeiCaiBaoPresent(this, this);
        meiCaiBaoPresent.payout1();

    }

    @OnClick({R.id.rl_common_title_back, R.id.btn_sure_payout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_common_title_back:
                finish();
                break;
            case R.id.btn_sure_payout:
                amount = editText.getText().toString().trim();
                if (TextUtils.isEmpty(amount)) {
                    toastShortShow("请输入您想转出的金额!");
                } else {
                    meiCaiBaoPresent.surePayOut(amount);

                }

                break;
        }
    }

    private void showDialog() {
        if (dialog == null) {
            dialog = new BaseDialog.Builder()
                    .layResuorce(R.layout.dialog_common)
                    .cancelViewClickEvent(R.id.tv_dialog_cancel)
                    .okViewClickEvent(R.id.tv_dialog_ok)
                    .confirmId(R.id.tv_dialog_confirm)
                    .llBottomId(R.id.ll_dialog)
                    .contentTextId(R.id.tv_dialog_content)
                    .hintTextId(R.id.tv_dialog_hint)
                    .gravityStyle(Gravity.CENTER)
                    .setHintText("转出成功")
                    .setContentText("您可以前往首页查看您的资产")
                    .showOneOrTwoBtn(false)
                    .setConfirmText("确定")
                    .gravityStyle(Gravity.CENTER)
                    .animResource(R.style.dialog_animation_fade)
                    .dialogWidth(-2)
                    .build();
            dialog.setOnDialogClickListener(new BaseDialog.OnDialogClickListener() {
                @Override
                public void onOkClick() {
                    dialog.dismiss();

                }

                @Override
                public void onOk2Click() {
                }
            });
        }
        dialog.show(getSupportFragmentManager(), TAG);
    }

    @Override
    public void onSuccess(Object object) {
        PayOut1Bean respone = (PayOut1Bean) object;
        balance = respone.getBtBalance();
        payout1Balance.setText(respone.getBtBalance() + "");
        SpConfig.getInstance().edit().putString(Constants.TOKEN, respone.getToken()).commit();

    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public int getOpt() {
        return 0;
    }

    @Override
    public void onSuccess2(Object object) {
        SurePayOutBean respone = (SurePayOutBean) object;
        if (respone.getError() == -1) {
            showDialog();
            balance = balance - Double.parseDouble(amount);
            payout1Balance.setText(balance + "");
            editText.setText("");
        }
        SpConfig.getInstance().edit().putString(Constants.TOKEN, respone.getToken()).commit();
    }

    @Override
    public void onError2(Throwable e) {

    }
}
